package site.hugboy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.hugboy.reggie.common.R;
import site.hugboy.reggie.dto.DishDto;
import site.hugboy.reggie.entity.Dish;
import site.hugboy.reggie.service.DishFlavorService;
import site.hugboy.reggie.service.DishService;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService ds;

    @Autowired
    private DishFlavorService dfs;

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());

        ds.saveWithFlavor(dishDto);
        return R.success("保存成功.");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        // 分页构造器
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        // 过滤条件
        qw.like(name != null, Dish::getName, name);
        // 排序条件
        qw.orderByDesc(Dish::getUpdateTime);

        // 执行 分页查询
        ds.page(pageInfo, qw);

        return R.success(pageInfo);
    }
}
